package yja.gzw.app.activity

import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.lifecycle.Observer
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import yja.gzw.app.R
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityHaoyouzhuyeBinding
import yja.gzw.app.fragmentview.FabuddFragment
import yja.gzw.app.fragmentview.GuanzhuFragment
import yja.gzw.app.fragmentview.TuijianFragment
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import com.google.android.material.tabs.TabLayout
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.qcloud.tuicore.TUIConstants
import com.tencent.qcloud.tuikit.tuichat.classicui.page.TUIC2CChatActivity
import yja.gzw.app.fragmentview.PingjiaFragment

class HaoyouzhuyeActivity :BaseActivity<Mainviewmodel, ActivityHaoyouzhuyeBinding>(R.layout.activity_haoyouzhuye){
    val list_fragment= mutableListOf<Fragment>(
        FabuddFragment(), GuanzhuFragment(), PingjiaFragment()
    )
    var listtlit= mutableListOf<String>("订单","动态","评价")

    override fun initView() {
        val id=intent.getStringExtra("id")
        UserData.idsss=id
        binding.guanzhu.setOnClickListener {
            viewmodel.onguanzhuhequguanwang(UserData.token, mapOf("user_id" to id!!))
        }


        viewmodel.onhaoyouzhuyewang(UserData.token, mapOf("id" to id!!))

       // Toast.makeText(this,id, Toast.LENGTH_SHORT).show()

        binding.fanhui.setOnClickListener {
            finish()

        }
        binding.xiaoxi.setOnClickListener {

            val intent = Intent(this, TUIC2CChatActivity::class.java)
            intent.putExtra(TUIConstants.TUIChat.CHAT_ID,id);
            intent.putExtra(TUIConstants.TUIChat.CHAT_TYPE, V2TIMConversation.V2TIM_C2C)
           startActivity(intent)

        }



        binding.viewpager.adapter=object : FragmentPagerAdapter(supportFragmentManager){
            override fun getCount(): Int {

                return list_fragment.size

            }

            override fun getItem(position: Int): Fragment {

                return list_fragment[position]

            }



        }
        binding.tabs.setupWithViewPager(binding.viewpager)

        binding.tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {


            }

            override fun onTabUnselected(tab: TabLayout.Tab) {


            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }


        })

        for (i in 0  until listtlit.size ){

            binding.tabs.getTabAt(i)?.setText(listtlit[i])



        }


    }

    override fun dataObservese() {

        viewmodel.onguanzhuhequguan.observe(this, Observer {
            if(it.code==1){

                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }


        })


        viewmodel.onhaoyouzhuye.observe(this, Observer {
            if(it.code==1){

                binding.name.text=it.data.nickname

                binding.shuliang.text=""+it.data.fav_count+"获赞     "+it.data.fans_count+"粉丝     "+it.data.focus_count+"关注"
                binding.idzhanshi.text="ID: "+it.data.id.toString()
                binding.gexiangqiamming.text=it.data.bio
                val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
                Glide.with(this).load("http://117ai.32.hzgqapp.com/"+it.data.avatar)
                    .apply(requestOptions)
                    .into(binding.touxiang)

            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

            }

        })



    }
}